#include <typescommon.h>
#include <errno.h>
#include <stdarg.h>
#include <ptcPackParse.h>
#include <ptcCommandOpt.h>
#include <deviceOperateSN.h>
#include <cmdSetReadSN.h>
#include <cmdReadBaseInform.h>
#include <sm4.h>

charQueueDefIstc(deviceReceiveQue, DEVICE_MCU_COMMON_STR_SIZE);
ptcCmdInstanceDef(devPtcCmd, deviceSnString, DEVICE_MCU_COMMON_STR_SIZE);

void sendPctResponseData(struct ptcCmdOptStruct *ptcCmdOpt)
{
    logHexData("resp pack:", ptcCmdOpt->parser.pkptr, ptcPackGetTotalSize(&ptcCmdOpt->parser));
}
void doPackSetReadSnData()
{
    uint8_t *tmpDataPtr;
    struct masterCmdStructName(SetReadSN) tmpMaster;
    ptcCmdInstanceDef(tmpPtcCmd, deviceSnString, DEVICE_MCU_COMMON_STR_SIZE);

    generatorRandHexBuffer(tmpMaster.setSn, sizeof(tmpMaster.setSn));
    // pack cmd head
    ptcCmdSetCmdNB(&tmpPtcCmd, ptcCmdFuncEnumDef(SetReadSN));
    ptcCmdSetCmdSn(&tmpPtcCmd, rand());
    ptcCmdSetCmdResp(&tmpPtcCmd, rand());
    ptcCmdSetUsrData(&tmpPtcCmd, &tmpMaster, sizeof(tmpMaster));
    // pack ptc head
    ptcPackSet485Addr(&tmpPtcCmd.parser, deviceSnString);
    ptcPackSetCrypte(&tmpPtcCmd.parser, protocolParseCrypte_None);
    ptcPackFinishData(&tmpPtcCmd.parser);
    // logdebug("%d", protocolGetTotalSize(&tmpPack));
    logHexData("wr sn pack:", tmpPtcCmd.parser.pkptr, ptcPackGetTotalSize(&tmpPtcCmd.parser));
    queueArrayInputString(&deviceReceiveQue, tmpPtcCmd.parser.pkptr, ptcPackGetTotalSize(&tmpPtcCmd.parser));
}
void doPackReadBaseInfo()
{
    uint8_t *tmpDataPtr;
    struct masterStructReadBaseInfo tmpMaster;
    ptcCmdInstanceDef(tmpPtcCmd, deviceSnString, DEVICE_MCU_COMMON_STR_SIZE);

    generatorRandHexBuffer(tmpMaster.remain, sizeof(tmpMaster.remain));
    // pack cmd head
    ptcCmdSetCmdNB(&tmpPtcCmd, ptcCmdFunc_ReadBaseInfo);
    ptcCmdSetCmdSn(&tmpPtcCmd, rand());
    ptcCmdSetCmdResp(&tmpPtcCmd, rand());
    ptcCmdSetUsrData(&tmpPtcCmd, &tmpMaster, sizeof(tmpMaster));
    // pack ptc head
    ptcPackSet485Addr(&tmpPtcCmd.parser, deviceSnString);
    ptcPackSetCrypte(&tmpPtcCmd.parser, protocolParseCrypte_None);
    ptcPackFinishData(&tmpPtcCmd.parser);
    // logdebug("%d", protocolGetTotalSize(&tmpPack));
    logHexData("read base info pack:", tmpPtcCmd.parser.pkptr, ptcPackGetTotalSize(&tmpPtcCmd.parser));
    queueArrayInputString(&deviceReceiveQue, tmpPtcCmd.parser.pkptr, ptcPackGetTotalSize(&tmpPtcCmd.parser));
}
void doParseData()
{
    uint8_t tmpvalue;
    while (queueArrayGetString(&deviceReceiveQue, &tmpvalue, 1) > 0)
    {
        ptcPackParseData(&devPtcCmd.parser, tmpvalue);
    }
}
#if 1
int main()
{
    deviceSnReadFormHardware();
    for (int i = 0; i < 2; i++)
    {
        uint8_t tmpstr[64];
        uint8_t tmpkey[16];
        generatorRandHexBuffer(tmpkey, sizeof(tmpkey));
        generatorRandHexBuffer(tmpstr, sizeof(tmpstr));
        logHexData("key str:", tmpkey, sizeof(tmpkey));
        logHexData("org str:", tmpstr, sizeof(tmpstr));
        sm4EncryptePro1(tmpstr, sizeof(tmpstr), tmpkey);
        logHexData("enc str:", tmpstr, sizeof(tmpstr));
        sm4DecryptePro1(tmpstr, sizeof(tmpstr), tmpkey);
        logHexData("dec str:", tmpstr, sizeof(tmpstr));
        doPackSetReadSnData();
        doParseData();
        doPackReadBaseInfo();
        doParseData();
    }
    return 0;
}
#endif
#if 0
#include <deviceNormalParam.h>

struct deviceParameterNormalInformOld
{
    struct
    {
        uint16_t paramSize;
        uint16_t paramCrc;
    };
    uint32_t prmT1;
    uint16_t prmT2;
};

struct deviceParameterNormalInformNew
{
    struct
    {
        uint16_t paramSize;
        uint16_t paramCrc;
    };
    uint32_t prmT1;
    uint16_t prmT2;
    float prmT3;
    uint8_t prmT4[10];
    uint32_t prmT5;
};

int main()
{
    struct deviceParameterNormalInformOld tmpparmOld;
    struct deviceParameterNormalInformNew tmpparmNew;
    char *tmpConstStr;
    // struct devParamInfoNorm *tmpNormalParam = paramNormInst();
    paramNormReset(0);
    paramNormPrint();
    memcpypro(&tmpparmOld, sizeof(tmpparmOld), paramNormInst(), sizeof(struct devParamInfoNorm));
    memcpypro(&tmpparmNew, sizeof(tmpparmNew), paramNormInst(), sizeof(struct devParamInfoNorm));

    tmpparmOld.prmT1 = 11;
    tmpparmOld.prmT2 = 12;
    paramCalcHeadInfo((struct devParamHeadInfo *)&tmpparmOld, paramGetDataSize(tmpparmOld));
    paramNormConfig(&tmpparmOld, 0);
    paramNormPrint();

    tmpparmNew.prmT1 = 21;
    tmpparmNew.prmT2 = 22;
    tmpparmNew.prmT3 = 23;
    memcpy(tmpparmNew.prmT4, "24-", 4);
    tmpparmNew.prmT5 = 25;
    paramCalcHeadInfo((struct devParamHeadInfo *)&tmpparmNew, paramGetDataSize(tmpparmNew));
    paramNormConfig(&tmpparmNew, 0);
    paramNormPrint();

    struct devParamInfoNorm tmpnormread;
    memcpy(&tmpnormread, paramNormInst(), sizeof(struct devParamInfoNorm));
    logHexData("parm norm data:", (const uint8_t *)&tmpnormread, sizeof(struct devParamInfoNorm));
    paramNormCvrtVal(&tmpnormread);
    logHexData("parm cvrt data:", (const uint8_t *)&tmpnormread, sizeof(struct devParamInfoNorm));
}
#endif
